package com.taldh.netty.io.bytebuf;

import com.taldh.netty.common.util.Logger;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.util.IllegalReferenceCountException;
import org.junit.Assert;
import org.junit.Test;

public class ReferenceTest {

    @Test
    public void testRef() {
        ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
        Logger.info("after create: " + buffer.refCnt());
        buffer.retain();
        Logger.info("after retain: " + buffer.refCnt());
        buffer.release();
        Logger.info("after release: " + buffer.refCnt());
        buffer.release();
        Logger.info("after release: " + buffer.refCnt());
        //错误：refCnt: 0, 不能再retain
        try {
            buffer.retain();
        } catch (Exception e) {
            Assert.assertTrue(e instanceof IllegalReferenceCountException);
        }

        Logger.info("after retain: " + buffer.refCnt());
    }

}
